#!/bin/bash

cd /root/kemp_images

images=()
images_all=(`ls -1 *.tar`)
images_num=${#images_all[@]}
 
images_list(){
echo 
echo "available images:"
echo  
for ((i=0; i<$images_num;i++))
    do 
    echo $i')  '${images_all[$i]}
done
echo
echo "a)   select all images"
echo "q)   quit"
echo  
}

images_select(){
images=()
echo "please select the images,use space to separate:"
read input_nums
echo
echo "selected:"
for i in $input_nums
    do
    echo $i  ${images_all[$i]}
    images[${#images[@]}]=${images_all[$i]}
done

echo 'please check,enter "y" to confirm,"n" to re-select,"q" to quit:'
read nums_check
if [ $nums_check = "y" ];then
    echo ok
fi
}

images_load(){
for image in ${images[*]}
do
    docker load -i $image
done
}

images_push(){
for image in ${images[*]}
do
    docker push registry/${image%.*}
done
}

images_load_and_push(){
for image in ${images[*]}
do
    docker load -i $image
    docker push registry/${image%.*}
done
}

echo "options:"
echo "0:  load and push images"
echo "1:  load images"
echo "2:  push images"
echo 
echo "please choose 0,1 or 2:"

read input

if [ $input = "0" ];then
    images_list
    images_select
echo ${images[*]}
    images_load_and_push
fi
